package com.wqiuqiu.gulimall.secdkill.scheduled;


import com.wqiuqiu.gulimall.secdkill.service.SecdKillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Slf4j
@Service
public class SeckKillSkuScheduled {

    @Autowired
    SecdKillService secdKillService;

    @Autowired
    RedissonClient redissonClient;

    private static final String upload_lock = "seckill:upload:lock";
    /**
     * 秒杀商品的定时上架
     * 每天晚上三点 上架最近三天的秒杀会话
     */

    @Scheduled(cron = "*/3 * * * * ?")
    public void uploadSku(){
        //TODO 存在重复上架 幂等性处理
        RLock rLock = redissonClient.getLock(upload_lock);
        rLock.lock(10, TimeUnit.SECONDS);
        try {
            secdKillService.uploadSeckillSku();
        }finally {
            rLock.unlock();
        }
        log.info("秒杀上架");
    }
}
